package com.clp.protocol.iec104.apdu.asdu.info_obj.info_elem;

import com.clp.protocol.core.utils.ByteUtil;
import io.netty.buffer.ByteBuf;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.Objects;


/**
 * 累积量（遥脉）
 */
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class M_IT_NA_1_InfoElem extends InfoElem{
    /**
     * 遥脉值为短浮点数
     */
    private long longVal; // TODO 暂时存为长整型

    @Override
    public M_IT_NA_1_InfoElem initBy(ByteBuf buf) {
        byte[] bytes = new byte[typeTag().getInfoElemBytesLen()];
        buf.readBytes(bytes);
        this.longVal = ByteUtil.bytes4ToLongLE(bytes);
        return this;
    }

    @Override
    public boolean isValid() {
        return true;
    }

    @Override
    public void writeTo(ByteBuf buf) {
        // TODO 暂时这样
        buf.writeBytes(new byte[]{0, 0, 0, 0});
    }

    @Override
    public String toString() {
        return "累积量：" + longVal;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        M_IT_NA_1_InfoElem that = (M_IT_NA_1_InfoElem) o;
        return longVal == that.longVal;
    }

    @Override
    public int hashCode() {
        return Objects.hash(longVal);
    }
}
